﻿<script>
            if(!location.hash.replace('#', '').length) {
                location.href = location.href.split('#')[0] + '#' + (Math.random() * 100).toString().replace('.', '');
                location.reload();
            }
</script>

<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="https://www.webrtc-experiment.com/CodecsHandler.js"></script>
<script src="https://www.webrtc-experiment.com/IceServersHandler.js"></script>
<script src="https://www.webrtc-experiment.com/meeting.js"> </script>
<button id="setup-new-meeting">Setup New Meeting</button>
        
<table style="border-left: 1px solid black; width: 100%;">
    <tr>
        <td>
            <h2 style="display: block; text-align: center;">
                Local Media Stream</h2>
            <section id="local-media-stream"></section>
        </td>
    </tr>
    <tr>
        <td style="border-top: 1px solid black;">
            <h2 style="display: block; text-align: center;">
                Remote Media Streams</h2>
            <section id="remote-media-streams"></section>
        </td>
    </tr>
</table>

<script>
    var meeting = new Meeting();

    var remoteMediaStreams = document.getElementById('remote-media-streams');
    var localMediaStream = document.getElementById('local-media-stream');

                // via: https://github.com/muaz-khan/WebRTC-Experiment/tree/master/websocket-over-nodejs
                meeting.openSignalingChannel = function(onmessage) {
                    var channel = location.href.replace(/\/|:|#|%|\.|\[|\]/g, '');
                    // wss://websocket-over-nodejs.herokuapp.com:443/
                    var websocket = new WebSocket('wss://webrtcweb.com:9449/');
                    websocket.onopen = function () {
                        websocket.push(JSON.stringify({
                            open: true,
                            channel: channel
                        }));
                    };
                    websocket.push = websocket.send;
                    websocket.send = function (data) {
                        if(websocket.readyState != 1) {
                            return setTimeout(function() {
                                websocket.send(data);
                            }, 300);
                        }
                        
                        websocket.push(JSON.stringify({
                            data: data,
                            channel: channel
                        }));
                    };
                    websocket.onmessage = function(e) {
                        onmessage(JSON.parse(e.data));
                    };
                    return websocket;
                };

    // on getting media stream
    meeting.onaddstream = function(e) {
        if (e.type == 'local') localMediaStream.appendChild(e.video);
        if (e.type == 'remote') remoteMediaStreams.insertBefore(e.video, remoteMediaStreams.firstChild);
    };

    // using firebase for signaling
    meeting.firebase = 'webrtc-experiment';

    // if someone leaves; just remove his video
    meeting.onuserleft = function(userid) {
        var video = document.getElementById(userid);
        if (video) video.parentNode.removeChild(video);
    };

    // check pre-created meeting rooms
    meeting.check('meeting-name');

    document.getElementById('setup-new-meeting').onclick = function() {
        // setup new meeting room
        meeting.setup('meeting-name');
        this.disabled = true;

        this.parentNode.innerHTML = '<h2><a href="' + location.href + '" target="_blank">Share this link</a></h2>';
    };
</script>
